//
//  BookmarkViewController.m
//  网易公开课项目
//
//  Created by 林 艺 on 13-11-10.
//  Copyright (c) 2013年 Marklin. All rights reserved.
//
//  收藏夹视图

#import "BookmarkViewController.h"
#import "CourseCell.h"
#import "CourseModel.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
#import "LoadCourseDataManager.h"

@interface BookmarkViewController ()

@end

@implementation BookmarkViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"我的收藏";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self _initBookmarkData];
    [self _initBarButtonItem];
    [self.tableView registerNib:[UINib nibWithNibName:@"CourseCell" bundle:nil] forCellReuseIdentifier:@"BookmarkCourseCell"];
}

- (void)_initBookmarkData
{
    LoadCourseDataManager *dataManager = [LoadCourseDataManager shareInstance];
    if (_bookmarkArray != nil)
    {
        [_bookmarkArray removeAllObjects];
        _bookmarkArray = nil;
    }
    _bookmarkArray = [NSMutableArray arrayWithArray:[dataManager loadBookmarkData]];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_initBarButtonItem
{
    //编辑按钮
    _editButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editButton setBackgroundImage:[UIImage imageNamed:@"btn_right.png"] forState:UIControlStateNormal];
    
    [_editButton setTitle:@"编辑" forState:UIControlStateNormal];

    _editButton.titleLabel.font = [UIFont systemFontOfSize:11];
    [_editButton addTarget:self action:@selector(editButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    _editButton.frame = CGRectMake(0, 0, 50, 34);
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:_editButton];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void)editButtonClicked:(UIButton *)button;
{
    BOOL flag = !self.tableView.editing;
    if (flag)
    {
        [button setTitle:@"完成" forState:UIControlStateNormal];
    }
    else
    {
        [button setTitle:@"编辑" forState:UIControlStateNormal];
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.editing = flag;
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _bookmarkArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BookmarkCourseCell"];
    CourseModel *model = [_bookmarkArray objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.title;
    cell.classLabel.text = model.tags;
    cell.playCountLabel.text = [NSString stringWithFormat:@"%@", model.playcount];
    cell.updatePlayCountLabel.text = [NSString stringWithFormat:@"%@", model.updatedPlaycount];
    [cell.courseImageView setImageWithURL:[NSURL URLWithString:model.imagePath]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 92;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    CourseModel *model = [_bookmarkArray objectAtIndex:indexPath.row];
    detailController.videoCode = model.plid;
    
    //隐藏tabbarView;
    UIView * view =[self.tabBarController valueForKey:@"tabbarView"];
    view.frame = CGRectMake(-320, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    
    [self.navigationController pushViewController:detailController animated:YES];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CourseModel *selectModel = [_bookmarkArray objectAtIndex:indexPath.row];
    LoadCourseDataManager *dataManager = [LoadCourseDataManager shareInstance];
    
    NSMutableArray *dataModels = [NSMutableArray arrayWithArray:[dataManager loadBookmarkData]];
    
    NSInteger index = 0;
    for (int i = 0; i < dataModels.count; i++)
    {
        CourseModel *model = dataModels[i];
        if (model.title == selectModel.title)
        {
            index = i;
            break;
        }
    }
    [dataModels removeObjectAtIndex:index];
    [dataManager writeDataToBookmarkArchiverWithArray:dataModels];
    [self _initBookmarkData];
    if (_bookmarkArray.count == 0)
    {
        self.noBookmarkView.hidden = NO;
        _editButton.hidden = YES;
        self.tableView.editing = NO;
        [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
    }
    else
    {
        self.noBookmarkView.hidden = YES;
        _editButton.hidden = NO;
    }

    [self.tableView reloadData];
}

#pragma mark --
#pragma mark viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
    [self _initBookmarkData];
    if (_bookmarkArray.count == 0)
    {
        self.noBookmarkView.hidden = NO;
        _editButton.hidden = YES;
        self.tableView.editing = NO;
        [_editButton setTitle:@"编辑" forState:UIControlStateNormal];
    }
    else
    {
        self.noBookmarkView.hidden = YES;
        _editButton.hidden = NO;
    }
    [self.tableView reloadData];
    
    UIView * view =[self.tabBarController valueForKey:@"tabbarView"];
    [UIView animateWithDuration:0.35 animations:^{
        view.frame = CGRectMake(0, view.frame.origin.y, view.frame.size.width, view.frame.size.height);
    }];
}


@end
